<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="keywords" content="">
  <meta name="description" content="">
  <title>文件上传</title>
  <link href="/static/css/bootstrap.min.css" rel="stylesheet"/>
  <link rel="stylesheet" href="/static/include/css/style.css">
  <style>
    .upload-box {
      padding: 20px;
    }

    #upload , #uploadFile{
      width: 0;
      opacity: 0;
      height: 0;
      filter: alpha(opacity=0)
    }
  </style>
</head>

<body class="hold-transition box box-main">
<div class="upload-box">
  <a> <label class="btn btn-success" for="uploadFile"> 选择文件</label> </a>
  <div id="filename"></div>
  <form enctype="multipart/form-data" id="upload" method="post" action="">
    <input type="file" class="" name="file" id="uploadFile"/>
  </form>
</div>


<script src="/static/include/js/footer.js"></script>
<script src="/static/js/jquery.form.min.js"></script>
<script>
  $(function () {
    $('#uploadFile').on('change', function () {
      if (isIE89){
        $('#filename').text($('#uploadFile').val())
      }else{
        $('#filename').text(document.getElementById('uploadFile').files[0].name)
      }

    })
  });

  function submitHandler(index, layero) {
    if (IEVersion()){
      var ajax_option = {
        url: api + "/admin/sys-file/ieUpload",
        // secureuri: false,
        type: "post",
        // fileElementId: 'uploadFile', //
        data: {
          "TENANT-ID": session.get('tenantId') ? session.get('tenantId') : '',
          "Authorization": 'Bearer ' + session.get('token')
        },
        dataType: "json",
        beforeSubmit: function () {
          $.modal.loading("上传中...");
        },
        success: function (result) {
          try {
            $.modal.closeLoading();
            $.modal.msgSuccess(!result.msg ? "上传成功" : result.msg);
            parent.refreshTable() //调用父页面函数
            layer.closeAll("loading");
            $.modal.close()
          }catch (e) {
            console.log('successError', e)
          }

        },
        error: function (data) {
          layer.alert("上传失败，请重新上传",{icon:2});
        }};
      $('#upload').ajaxSubmit(ajax_option);
      // $.ajaxFileUpload(ajax_option)
    }else{
      var formdata = new FormData();
      formdata.append("file", document.getElementById('uploadFile').files[0]);
      $.ajax({
        url: api + "/admin/sys-file/upload",
        data: formdata,
        type: "post",
        processData: false,
        contentType: false,
        beforeSend: function () {
          $.modal.loading("上传中...");
        },
        success: function (result) {
          $.modal.closeLoading();
          $.modal.msgSuccess(!result.msg ? "上传成功" : result.msg);
          $.modal.close()
          parent.refreshTable();
        }
      })
    }


  }

  //判断ie浏览器版本 以ie10为分界，ie10以下返回true，否则返回false
  function IEVersion() {
    //取得浏览器的userAgent字符串
    var userAgent = navigator.userAgent;
    //判断是否IE浏览器
    var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1;
    if (isIE) {
      var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
      reIE.test(userAgent);
      var fIEVersion = parseFloat(RegExp["$1"]);
      if (fIEVersion < 10) {
        return true;
      }
    } else {
      return false;
    }
  }


</script>
</body>

</html>